<?php
  define('IN_ECS', true);
  
  define('ROOT_PATH', str_replace('admin/category_list.php', '', str_replace('\\', '/', __FILE__)));
  require_once(ROOT_PATH.'ty/TyUtils.php');
  require_once(ROOT_PATH.'ty/admin/categoryService.php');
  
  $smarty = TyUtils::getSmarty();
  $cateService = new CategoryService();
  
  $action  = isset($_REQUEST['act']) ? trim($_REQUEST['act']) : 'category_list';
  
  if($action == 'category_list'){
      
      $cateList = $cateService->getAllCategoryList();
      $smarty->assign('cate_list', $cateList);
      
      $cateOptionHtml = $cateService->buildCateOptionHtml(null);
      $smarty->assign('cate_option', $cateOptionHtml);
      
      $content = $smarty->fetch(ROOT_PATH.'templates/admin/category_list.inc');
      
      exit($content);
  }
  elseif($action == 'save_cate'){
      $cateInfo['cate_id'] = isset($_REQUEST['cate_id']) ? trim($_REQUEST['cate_id']) : '';
      $cateInfo['cate_title'] = isset($_REQUEST['cate_title']) ? addslashes(trim($_REQUEST['cate_title'])) : '';
      $cateInfo['is_show_index'] = isset($_REQUEST['is_show_index']) ? trim($_REQUEST['is_show_index']) : '0';
      $cateInfo['parent_id'] = isset($_REQUEST['parent_id']) ? trim($_REQUEST['parent_id']) : '0';
      $cateInfo['flag'] = isset($_REQUEST['flag']) ? trim($_REQUEST['flag']) : '1';
      $cateInfo['open_news'] = isset($_REQUEST['open_news']) ? trim($_REQUEST['open_news']) : '0';
      $cateInfo['order_no'] = isset($_REQUEST['category_order']) && !empty($_REQUEST['category_order']) ? trim($_REQUEST['category_order']) : '0';
      
      $modifyRows = 0;
      if(empty($cateInfo['cate_id'])){
          $modifyRows = $cateService->saveCategory($cateInfo);
      }else{
          $modifyRows = $cateService->updateCategory($cateInfo);
      }
      exit($modifyRows);
  }
  elseif($action == 'delete_cate'){
      $cateId = $_REQUEST['cate_id'];
      $delRows = 0;
      if(!empty($cateId)){
          $sql = "delete from news_category where cate_id=$cateId";
          $delRows = $cateService->getDb()->query($sql);
      }
      exit($delRows);
  }
  
?>
